package controllers

import (
	//"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/cache"
)

var varCache cache.Cache

type commonController struct {
	beego.Controller
	cache cache.Cache
}

func init() {
	//初始化缓存
	varCache, _ = cache.NewCache("memory", `{"interval":6000}`)

	//权限处理，接口验证
}

//预处理
func (this *commonController) Prepare() {
	this.cache = varCache
}

//返回结果
func (this *commonController) Rsp(status int, msg string) {
	this.Data["json"] = &map[string]interface{}{"status": status, "message": msg}
	this.ServeJSON()
}

//返回结果，带data
func (this *commonController) RspData(status int, msg string, data string) {
	this.Data["json"] = &map[string]interface{}{"status": status, "message": msg, "data": data}
	this.ServeJSON()
}

//跨域
func (c *commonController) AllowCross() {
	//c.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "http://127.0.0.1:8080") //允许访问源
	c.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
	//c.Ctx.ResponseWriter.Header().Add("Access-Control-Allow-Headers", "Content-Type")            //header的类型                    //允许访问源
	c.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET") //允许post访问
	//c.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "Content-Type,Authorization") //header的类型
	//c.Ctx.ResponseWriter.Header().Set("Access-Control-Max-Age", "1728000")
	//c.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Credentials", "true")
	//c.Ctx.ResponseWriter.Header().Set("content-type", "application/json") //返回数据格式是json
}
